package com.timothy.android.answer;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.timothy.android.BaseAnswerActivity;
import com.timothy.android.util.ValueCopyUtil;
import com.timothy.chart.Bar2D;
import com.timothy.chart.BaseChart;
import com.timothy.chart.CanvasSetting;
import com.timothy.chart.NoGraph;
import com.timothy.chart.Pie2D;
import com.timothy.chart.Pie3D;

public class NoGraphAnswerActivity extends BaseAnswerActivity{
	private final static String LOG_TAG = "NoGraphAnswerActivity";
	List<NoGraph> noGraphList = new ArrayList<NoGraph>();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setTitle("NoGraph");
		
		Log.i(LOG_TAG,"-------------------onCreate-------------------");
		
		for(int i=0;i<chartList.size();i++){
			Log.i(LOG_TAG,"Loop chartList for PieList");
			
			BaseChart baseChart = chartList.get(i);
			Log.i(LOG_TAG,baseChart.toString());
			
//			NoGraph noGraph = setValues(baseChart);
			
			NoGraph noGraph = new NoGraph();
			try {
				ValueCopyUtil.valueCopy(baseChart, noGraph);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			Log.i(LOG_TAG,noGraph.toString());
			
//			if(screenSize == 1){
//				noGraph.setStartX(100);
//				noGraph.setStartY(i*200+100);
//			}
//			if(screenSize == 2){
//				noGraph.setStartX(200);
//				noGraph.setStartY(i*200+100);					
//			}
//			if(screenSize == 3){
//				noGraph.setStartX(200);
//				noGraph.setStartY(i*200+100);					
//			}

			noGraphList.add(noGraph);
		}
		
		
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		VerticalNoGraphView view = new VerticalNoGraphView(this);
		setContentView(view);
	}
	
	class VerticalNoGraphView extends SurfaceView implements SurfaceHolder.Callback{
		private SurfaceHolder mHolder;
		private Canvas canvas;
		
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			recoverBrightness();
			return super.onTouchEvent(event);
		}
		
		public VerticalNoGraphView(Context context) {
			super(context);
			mHolder = getHolder();
			mHolder.addCallback(this);
		}

		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			canvas = mHolder.lockCanvas();
			
			int canvasHeight = canvas.getHeight();
			int canvasWidth = canvas.getWidth();
			Log.i(LOG_TAG, "VerticalNoGraphView:Canvas Height:"+canvasHeight);
			Log.i(LOG_TAG, "VerticalNoGraphView:Canvas Width:"+canvasWidth);
			
			CanvasSetting setting = new CanvasSetting(canvasHeight,canvasWidth,Color.BLACK,1);
			canvas.drawColor(setting.getBgColor());// 设置画布背景为黑色
			
			//write question
			Paint mPaint = new Paint();
			mPaint.setColor(Color.parseColor(questionTextColor));		
			mPaint.setTextSize(textSize);
			int queX = setting.getLeftMargin();
			int queY = setting.getTopMargin();
			Log.i(LOG_TAG, "VerticalNoGraphView:queX:"+queX);
			Log.i(LOG_TAG, "VerticalNoGraphView:queY:"+queY);
			canvas.drawText(answer.getQuestion().getQuestionName(),queX,queY,mPaint); 
			canvas.save();
			
			int canvasNoMarginHeight = canvasHeight - setting.getTopMargin() - setting.getBottomMargin();
			int barHeightInclSpace = canvasNoMarginHeight/noGraphList.size();
//			int barHeight = (int)(barHeightInclSpace*0.9);
//			int spaceHeight = (int)(barHeightInclSpace*0.1);
//			if(spaceHeight<1) spaceHeight = 1;
			
			for(int i=0;i<noGraphList.size();i++){
				NoGraph noGraph = noGraphList.get(i);
				
				int tlX = setting.getLeftMargin();
				int tlY = (int)(setting.getTopMargin()*1.3)+ barHeightInclSpace*i;
				
				mPaint.setTextSize(noGraph.getChoiceTextSize());
				mPaint.setColor(Color.parseColor(noGraph.getChoiceTextColor()));
				
				canvas.drawText(noGraph.toMessage(), tlX, tlY, mPaint);
				canvas.save();
			}

			canvas.restore();
			mHolder.unlockCanvasAndPost(canvas);
			
		}

		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
}

